package com.vote.admin.service;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.google.common.collect.Lists;
import com.vote.common.constant.RedisCacheTime;
import com.vote.common.constant.RedisConstants;
import com.vote.common.entity.VoteMenu;
import com.vote.common.entity.VoteUser;
import com.vote.common.enums.AdminSessionEnum;
import com.vote.common.enums.UserTypeEnum;
import com.vote.common.returns.RespCode;
import com.vote.common.returns.RespEntity;
import com.vote.common.service.VoteMenuService;
import java.util.List;

import com.vote.common.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

@Slf4j
@Service
public class AdminMainService {

    @Autowired
    private VoteMenuService voteMenuService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 菜单列表
     * @param session
     * @return
     */
    public RespEntity<List<VoteMenu>> queryAllMenu(HttpSession session) {
        VoteUser voteUser = (VoteUser) session.getAttribute(AdminSessionEnum.ADMIN.getCode());
        //读取redis缓存数据
        String value = null;
        if (UserTypeEnum.SUPER_ADMIN.getCode().equals(voteUser.getType())) {
            value = redisUtil.get(RedisConstants.Key.SUPER_ADMIN_MENU);
        }else {
            value = redisUtil.get(RedisConstants.Key.ADMIN_MENU);
        }

        //如果缓存为空 ，则查询数据库 并新建缓存
        if (StringUtils.isBlank(value)) {
            List<VoteMenu> voteMenuList = Lists.newArrayList();
            if (UserTypeEnum.SUPER_ADMIN.getCode().equals(voteUser.getType())) {
                voteMenuList = voteMenuService.queryAllMenus(null);
                redisUtil.set(RedisConstants.Key.SUPER_ADMIN_MENU, JSONUtil.toJsonStr(voteMenuList), RedisCacheTime.SUPER_ADMIN_MENU_TIME);
            }else {
                voteMenuList = voteMenuService.queryAllMenus(voteUser.getType() + "");
                redisUtil.set(RedisConstants.Key.ADMIN_MENU, JSONUtil.toJsonStr(voteMenuList), RedisCacheTime.ADMIN_MENU_TIME);
            }
            return new RespEntity(RespCode.SUCCESS, voteMenuList);
        }

        //缓存不为空 则使用缓存数据
        JSONArray jsonArray = JSONUtil.parseArray(value);
        List<VoteMenu> menuList = JSONUtil.toList(jsonArray, VoteMenu.class);
        return new RespEntity(RespCode.SUCCESS, menuList);

    }

    /**
     * 欢迎语
     * @param session
     * @return
     */
    public RespEntity<String> welcome(HttpSession session) {
        VoteUser voteUser = (VoteUser) session.getAttribute(AdminSessionEnum.ADMIN.getCode());
        return new RespEntity<>(RespCode.SUCCESS, voteUser.getName());
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    public RespEntity signOut(HttpSession session) {
        session.removeAttribute(AdminSessionEnum.ADMIN.getCode());
        return new RespEntity(RespCode.SUCCESS);
    }

}
